package com.yanzhengwu.boot.core.druid;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;

import java.util.Properties;

/**
 * TODO 切换数据源时 可以通过 connection-properties 配置文件属性进行 一些操作 比如对数据库或密码的解密
 * TODO 配置文件中通过 password-callback: com.yanzhengwu.boot.core.druid.MyPasswordCallback 指定类的全限定名调用
 *
 * @author yanzhengwu
 * @create 2022-11-18 16:44
 */
public class DynamicPasswordCallback extends DruidPasswordCallback {


    @Override
    public void setProperties(Properties properties) {
        //System.out.println("密码回调方法执行");
        super.setProperties(properties);
        String password = properties.getProperty("password");
        String publicKey = properties.getProperty("config.decrypt.key");
        try {
            String dbPassword = ConfigTools.decrypt(publicKey, password);
            setPassword(dbPassword.toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /*@Override
    public void setPassword(char[] password) {
        super.setPassword(password);
    }*/
}
